package cn.com.hiocde.myresume.fragment;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import cn.com.hiocde.myresume.R;

/**
 * Created by Hmqi on 2016/5/31.
 */
public class ShowPlatformFragment extends Fragment{
    private WebView webView=null;
    private ProgressBar pb=null;
    BroadcastReceiver connectionReceiver=null;
    private String url="http://m.weibo.cn/u/5143027608";

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_home_view2,container,false);
        webView = (WebView) view.findViewById(R.id.webview);
        pb=(ProgressBar)view.findViewById(R.id.weibo_pb);//***auto-adapt motivation of progressBar

        openLink(url);
        monitorNetwork();

        return view;
    }

    public void openLink(String url){
        WebSettings ws = webView.getSettings();
        ws.setJavaScriptEnabled(true);
//        ws.setSupportZoom(true);
//        ws.setBuiltInZoomControls(true);

        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,String url)
            {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url)
            {
                super.onPageFinished(view, url);
                pb.setVisibility(View.GONE);
                webView.setVisibility(View.VISIBLE);
//                if (webView.getContentHeight() != 0) {
//                    //***turely finished , has rendered
                //no use! skipped
//                }
            }
        });
//        webView.setWebChromeClient(new WebChromeClient(){
//            @Override
//            public void onProgressChanged(WebView view, int newProgress) {
//                if (newProgress == 100) {
////                    pb.setVisibility(View.GONE);
////                    webView.setVisibility(View.VISIBLE);
//                } else {
////                    if (pb.getVisibility() == View.GONE)
////                        pb.setVisibility(View.VISIBLE);
//                    pb.setProgress(newProgress);
//                }
//                super.onProgressChanged(view, newProgress);
//            }
//        });
        webView.loadUrl(url);
    }

    public void monitorNetwork(){
        final Activity home=getActivity();

        connectionReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                ConnectivityManager connectMgr = (ConnectivityManager) home.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
                NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
                if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
                    Toast.makeText(home,"Connection is unavailable! please open data connection.",Toast.LENGTH_LONG).show();
                }else {
                    openLink(url);
                }
            }
        };

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        home.registerReceiver(connectionReceiver, intentFilter);
    }

    public WebView getWebView(){
        return webView;
    }

    public BroadcastReceiver getConnectionReceiver(){return connectionReceiver;}

}
